package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {

        User user = userMapper.findByUserName(username);

        return user;
    }

    /**
     * 用户管理分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<User> list = userMapper.findPage(queryPageBean.getQueryString());
        PageInfo<User> userPageInfo = new PageInfo<>(list);
        return new PageResult(userPageInfo.getTotal(), userPageInfo.getList());
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        Integer count = userMapper.findUserAndUser_RoleAndRoleById(id);
        if (count != 0) {
            throw new RuntimeException("不可删除");
        }
        userMapper.deleteById(id);
    }

    /**
     * 显示添加权限
     *
     * @return
     */
    @Override
    public List<Role> findRole() {
        return userMapper.findRole();
    }

    /**
     * 添加用户
     *
     * @param user
     * @param checkitemIds
     */
    @Override
    public void add(User user, Integer[] checkitemIds) {
        String name = null;
        try {
            name = userMapper.findNameByName(user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (name != null) {
            throw new RuntimeException("用户名存在，请重新输入");
        }

        String pwd = MD5Utils.md5(user.getPassword());
        user.setPassword(pwd);
        userMapper.addUser(user);
        if (checkitemIds.length != 0) {
            userMapper.addUserAndRole(user.getId(), checkitemIds);
        }
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findRoleAndUserById(Integer id) {
        List<Integer> num = userMapper.findRoleAndUserById(id);
        return num;
    }

    /**
     * 编辑修改
     * @param user
     * @param checkitemIds
     */
    @Override
    public void edit(User user, Integer[] checkitemIds) {
        String pwd = MD5Utils.md5(user.getPassword());
        user.setPassword(pwd);
        userMapper.editUser(user);
        if (checkitemIds.length != 0) {
            userMapper.delectUserAndRole(user.getId());
            userMapper.editUserAndRole(user.getId(), checkitemIds);
        }
    }
}
